package org.fuxia.im.core.entries;

import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Map;

import org.fuxia.im.core.iconst.ServerConst;
/**
 * 服务器为每个连接上来的客户端创建的一个会话实例
 *
 */
public class ECSession
{
	private static final String	INET_ADDRESS	= "INET_SOCKET_ADDRESS";
	private static final String	CONNECTED_USER	= "CONNECTED_USER";
	private static final String	IS_DISPOSED		= "IS_DISPOSED";
	private Map<String, Object>	attributes		= new HashMap<String, Object>();

	public Object getAttribute(String key)
	{
		return attributes.get(key);
	}

	public void setAttribute(String key , Object value)
	{
		attributes.put(key, value);
	}

	public void setInetAddress(String ipAddress)
	{
		setAttribute(INET_ADDRESS, ipAddress);
	}

	public String getInetAddress()
	{
		Object obj = this.getAttribute(INET_ADDRESS);
		return obj == null ? null : obj.toString();
	}

	public String getConnectedUser()
	{
		Object obj = this.getAttribute(CONNECTED_USER);
		return obj == null ? null : (String) obj;
	}

	public void setConnectedUser(String userCode)
	{
		this.setAttribute(CONNECTED_USER, userCode);
	}

	public void setDisposed(boolean bool)
	{
		this.setAttribute(IS_DISPOSED, bool);
	}

	public boolean getDisposed()
	{
		Object obj = this.getAttribute(IS_DISPOSED);
		return obj == null ? true : ((Boolean) obj).booleanValue();
	}

	@Override
	public String toString()
	{
		return "ECSession [attributes=" + attributes + "]";
	}
	
	public SocketChannel getChannel() {
		Object obj = this.getAttribute(ServerConst.CHANNEL.name()) ;
		return obj == null ?null :(SocketChannel)obj ;
	}

}
